package recursion;

/**
 * @Classname JZ46把数字翻译成字符串
 * @Description TODO
 * @Date 2023/3/7 22:08
 * @Created by xjl
 */
public class JZ46把数字翻译成字符串 {

    public int translateNum(int num) {
        char[] chars = String.valueOf(num).toCharArray();
        int index = 0;
        return next(chars, index);
    }

    /**
     * @description 就是可能等于的一个数或者两个字母组合一个数字
      * @param: chars
     * @param: index
     * @date: 2023/3/7 22:08
     * @return: int
     * @author: xjl
    */
    private int next(char[] chars, int index) {
        if (index > chars.length - 1) {
            return 1;
        }
        int one = next(chars, index + 1);
        int two = 0;
        if (index < chars.length - 1 && ((chars[index] - '0') * 10 + (chars[index + 1] - '0') > 9 && (chars[index] - '0') * 10 + (chars[index + 1] - '0') < 26)) {
            two = next(chars, index + 2);
        }
        return one + two;
    }
}
